package top.hkyzf.study.j2se.juc.locks;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 题目：实现A->B->C三个线程启动。
 * AA打印5次，BB打印10次，CC打印15次
 * 紧接着
 * AA打印5次，BB打印10次，CC打印15次
 * ......
 * 来十轮
 *
 * @author 朱峰
 * @date 2021-3-15 17:53
 */
public class SyncAndReentrantLockDemo1 {

    public static void main(String[] args) {
        ShareResource1 resource = new ShareResource1();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resource.printN(5, resource.c1);
            }
        }, "AA").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resource.printN(10, resource.c2);
            }
        }, "BB").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resource.printN(15, resource.c3);
            }
        }, "CC").start();
    }
}

class ShareResource1 {
    private int number = 1; // A:1 B:2 C:3
    private Lock lock = new ReentrantLock();
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();

    /**
     * 打印5次
     */
    public void print5() {
        lock.lock();
        try {
            // 1、判断
            while (number != 1) {
                // 等待
                c1.await();
            }
            // 2、干活
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 3、 通知
            number = 2;
            c2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 打印10次
     */
    public void print10() {
        lock.lock();
        try {
            // 1、判断
            while (number != 2) {
                // 等待
                c2.await();
            }
            // 2、干活
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 3、 通知
            number = 3;
            c3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 打印15次
     */
    public void print15() {
        lock.lock();
        try {
            // 1、判断
            while (number != 3) {
                // 等待
                c3.await();
            }
            // 2、干活
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 3、 通知
            number = 1;
            c1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 打印n次
     * @param n
     */
    public void printN(Integer n, Condition condition) {
        lock.lock();
        try {
            // 1、判断
            while (number == 1 && condition.equals(c1)) {
                // 等待
                condition.await();
            }
            // 1、判断
            while (number == 2 && condition.equals(c2)) {
                // 等待
                condition.await();
            }
            // 1、判断
            while (number == 3 && condition.equals(c3)) {
                // 等待
                condition.await();
            }
            // 2、干活
            for (int i = 1; i <= n; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            // 3、 通知
            while (number == 1) {
                number = 2;
                c2.signal();
            }
            // 3、 通知
            while (number == 2) {
                number = 3;
                c3.signal();
            }
            // 3、 通知
            while (number == 3) {
                number = 1;
                c1.signal();
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}